package ro.gateway.aida.fnd;

import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import ro.xblue.translator.LanguageBean;
import ro.xblue.translator.ShowMessageTag;
import ro.xblue.translator.TranslatorUtils;



/**
 * <p>Title: Romanian AIDA</p>
 * <p>Description: :D application</p>
 * <p>Copyright: Copyright (comparator) 2003</p>
 * <p>Company: Romania Development Gateway </p>
 * @author Mihai Popoaei, mihai_popoaei@yahoo.com, smike@intellisource.ro
 * @version 1.0-* @version $Id: FundingToHtml.java,v 1.1 2004/10/24 23:37:14 mihaipostelnicu Exp $
 */
public class FundingToHtml
{
      public static final String fundingItemToHTML(
            FundingItem item,
            String lang,
            String tableStyleClass,
            String cellStyleClass,
            ServletContext application,
            HttpServletRequest request )
      {
            LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage( request, application );

            String msg_bycat = ShowMessageTag.
                  getMessage( application, "fundings", "tags.by_cat", lang_bean.language );
            if ( msg_bycat == null ) msg_bycat = DEF_msg_bycat;
            String msg_yearly = ShowMessageTag.
                  getMessage( application, "fundings", "tags.yearly", lang_bean.language );
            if ( msg_yearly == null ) msg_yearly = DEF_msg_yearly;

            StringBuffer sb = new StringBuffer();
            sb.append( "<table width=\"100%\" class=\"" ).
                  append( tableStyleClass ).
                  append( "\">" );
            // organization, amount, currency
            sb.append( "<tr><td colspan=\"2\" class=\"" ).
                  append( cellStyleClass ).
                  append( "\">" ).
                  append( ( item.getOrg() != null ) ? item.getOrg().getName().toString() : "Org:unspecified" ).
                  append( ":" ).
                  append( item.getAmount() ).
                  append( " " ).
                  append( item.getCurrency() != null ? item.getCurrency() : "Currency:unspeecified" );
            sb.append( "</td></tr>" );

            // type, status
            sb.append( "<tr><td colspan=\"2\" class=\"" ).
                  append( cellStyleClass ).
                  append( "\">" ).
//                  append( ( item.getType() != null ) ? item.getType().getName( lang ) : "Type:unspecified" ).
//                  append( "," ).
                  append( ( item.getStatus() != null ) ? item.getStatus().getString( lang ) : "Status:unspecified" ).
                  append( "</td></tr>" );

            FundingByCatItem[] byCatItems = item.getByCatItemsArray();
            if ( byCatItems == null ) byCatItems = new FundingByCatItem[ 0 ];
            FundingYearlyItem[] yearlyItems = item.getYearlyItemsArray();
            if ( yearlyItems == null ) yearlyItems = new FundingYearlyItem[ 0 ];
            int nrSubItems =
                  ( ( byCatItems.length != 0 ) ? byCatItems.length + 1 : 0 ) +
                  ( ( yearlyItems.length != 0 ) ? yearlyItems.length + 1 : 0 );
            if ( nrSubItems > 0 )
            {
                  nrSubItems++;
                  sb.append( "<tr><td rowspan=\"" ).
                        append( nrSubItems ).
                        append( "\" width=\"10%\" class=\"" ).
                        append( cellStyleClass ).
                        append( "\">&nbsp;</td></tr>" );
                  if ( byCatItems.length > 0 )
                  {
                        sb.append( "<tr><td width=\"90%\" class=\"" ).
                              append( cellStyleClass ).
                              append( "\"><u>" ).
                              append( msg_bycat ).
                              append( "</u></td></tr>" );
                        for ( int i = 0; i < byCatItems.length; i++ )
                        {
                              sb.append( "<tr><td class=\"" ).
                                    append( cellStyleClass ).
                                    append( "\">" ).
                                    append( FundingToHtml.fundingByCatItemToHTML(
                                          byCatItems[ i ],
                                          lang,
                                          tableStyleClass,
                                          cellStyleClass, application, request ) ).
                                    append( "</td></tr>" );
                        }
                  }
                  if ( yearlyItems.length > 0 )
                  {
                        sb.append( "<tr><td class=\"" ).
                              append( cellStyleClass ).
                              append( "\"><u>" ).
                              append( msg_yearly ).
                              append( "</u></td></tr>" );
                        for ( int i = 0; i < yearlyItems.length; i++ )
                        {
                              sb.append( "<tr><td class=\"" ).
                                    append( cellStyleClass ).
                                    append( "\">" ).
                                    append( FundingToHtml.fundingYearlyItemToHTML(
                                          yearlyItems[ i ],
                                          lang,
                                          tableStyleClass,
                                          cellStyleClass ) ).
                                    append( "</td></tr>" );
                        }
                  }
            }

            sb.append( "</table>" );
            return sb.toString();
      }

      public static final String fundingYearlyItemToHTML(
            FundingYearlyItem item,
            String lang,
            String tableStyleClass,
            String cellStyleClass )
      {

            StringBuffer sb = new StringBuffer();
            sb.append( "<table width=\"100%\" class=\"" ).
                  append( tableStyleClass ).
                  append( "\">" );
            // organization, amount, currency
            sb.append( "<tr><td class=\"" ).
                  append( cellStyleClass ).
                  append( "\">" ).
                  append( ( item.getStart_date() != -1 ) ? formatDate( item.getStart_date() ) : "Date:unspecified" ).
                  append( ":" ).
                  append( item.getAmount() ).
                  append( " " ).
                  append( item.getCurrency() != null ? item.getCurrency() : "Currency:unspecified" );
            sb.append( "</td></tr>" );

            sb.append( "</table>" );
            return sb.toString();
      }

      public static final String fundingByCatItemToHTML(
            FundingByCatItem item,
            String lang,
            String tableStyleClass,
            String cellStyleClass,
            ServletContext application,
            HttpServletRequest request )
      {
            LanguageBean lang_bean = TranslatorUtils.getCurrentLanguage( request, application );

            String msg_bycat = ShowMessageTag.
                  getMessage( application, "fundings", "tags.by_cat", lang_bean.language );

            String msg_yearly = ShowMessageTag.
                  getMessage( application, "fundings", "tags.yearly", lang_bean.language );
            if ( msg_yearly == null ) msg_yearly = DEF_msg_yearly;

            StringBuffer sb = new StringBuffer();
            sb.append( "<table width=\"100%\" class=\"" ).
                  append( tableStyleClass ).
                  append( "\">" );
            // organization, amount, currency
            sb.append( "<tr><td colspan=\"2\" class=\"" ).
                  append( cellStyleClass ).
                  append( "\">" ).
                  append( ( item.getComponent() != null ) ? item.getComponent().getName( lang ) : "EditCategoryForm:unspecified" ).
                  append( ":" ).
                  append( item.getAmount() ).
                  append( " " ).
                  append( item.getCurrency() != null ? item.getCurrency() : "Currency:unspecified" );
            sb.append( "</td></tr>" );

            FundingYearlyItem[] yearlyItems = item.getYearlyItemsArray();
            if ( yearlyItems == null ) yearlyItems = new FundingYearlyItem[ 0 ];
            int nrSubItems = ( ( yearlyItems.length != 0 ) ? yearlyItems.length + 1 : 0 );
            if ( nrSubItems > 0 )
            {
                  nrSubItems++;
                  sb.append( "<tr><td rowspan=\"" ).
                        append( nrSubItems ).
                        append( "\" width=\"10%\" class=\"" ).
                        append( cellStyleClass ).
                        append( "\">&nbsp;</td></tr>" );
                  if ( yearlyItems.length > 0 )
                  {
                        sb.append( "<tr><td class=\"" ).
                              append( cellStyleClass ).
                              append( "\"><u>" ).
                              append( msg_yearly ).
                              append( "</u></td></tr>" );
                        for ( int i = 0; i < yearlyItems.length; i++ )
                        {
                              sb.append( "<tr><td width=\"90%\" class=\"" ).
                                    append( cellStyleClass ).
                                    append( "\">" ).
                                    append( FundingToHtml.fundingYearlyItemToHTML(
                                          yearlyItems[ i ],
                                          lang,
                                          tableStyleClass,
                                          cellStyleClass ) ).
                                    append( "</td></tr>" );
                        }
                  }
            }

            sb.append( "</table>" );

            return sb.toString();
      }

      public static final String formatDate( long date )
      {
            GregorianCalendar cal = new GregorianCalendar();

            cal.setTimeInMillis( date );
            SimpleDateFormat df = new SimpleDateFormat( "dd/MM/yyyy" );

            return df.format( cal.getTime() );
      }

      public static final String DEF_msg_yearly = "Funding yearly";
      public static final String DEF_msg_bycat = "Funding by orgType";

}
